package src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine;

import src.edu.wayne.cs.csc4110.hw6.implementations.dbase.UserImpl;
import src.edu.wayne.cs.csc4110.hw6.interfaces.User;

public class ChangeToNextUserState extends AbstractState {
	
	ChangeToNextUserState(StateMachine stateMachine){
		super(stateMachine);
	}
	
	@Override
	public void goToLoggedInState(){
		int count = 0;
		for(User user: stateMachine.getUsersInSession())
		{
			if(user != null){
				count++;
			}
		}
		
		User [] users = new UserImpl[count];
		count = 0;
		for(User user: stateMachine.getUsersInSession())
		{
			if(user != null){
				users[count] = user;
				count++;
			}
		}
		
		
		if ( users.length > 1 )
		{
			User currentUser = stateMachine.getCurrentUser();		
			
			int i;
			int index = -1;
			
			for (i=0;i < users.length;i++)
			{
				if ( users[i].equals(currentUser) ){
					index = i;
					break;
				}
			}
			
			if (index < (users.length - 1))
			{
				currentUser = users[index + 1];
			}
			else
			{
				currentUser = users[0];
			}
			
			stateMachine.setCurrentUser(currentUser);
		}
		
		stateMachine.setState(stateMachine.getLoggedInState());
		stateMachine.goToLoggedInState();
	}

}
